home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 / Ham Radio 2000.iso / ham2000 / tcp_ip / ntp_src / ntp.h < prev    next >
C/C++ Source or Header  |  1992-04-03  |  23KB  |  663 lines

  1. /*
  2.  * ntp.h - NTP definitions for the masses
  3.  */
  4.  
  5. #ifndef _NETUSER_H
  6. #include "netuser.h"
  7. #endif
  8.  
  9. #ifndef _UDP_H
  10. #include "udp.h"
  11. #endif
  12.  
  13. #ifndef _TIMER_H
  14. #include "timer.h"
  15. #endif
  16.  
  17. /*
  18.  * How to get signed characters.  On machines where signed char works,
  19.  * use it.  On machines where signed char doesn't work, char had better
  20.  * be signed.
  21.  */
  22. #if defined(NO_SIGNED_CHAR_DECL)
  23. typedef char s_char;
  24. #else
  25. typedef signed char s_char;
  26. #endif
  27.  
  28. /*
  29.  * ntohs, etc., are not part of NOS.  Rather than hack all the ntp code
  30.  * to use the equivalents in NOS, I have put the functions into ntp_util.c
  31.  */
  32. extern int16 htons __ARGS((int16));
  33. extern int16 ntohs __ARGS((int16));
  34. extern int32 htonl __ARGS((int32));
  35. extern int32 ntohl __ARGS((int32));
  36.  
  37. /*
  38.  * NOS does not include bzero() either. 
  39.  * Use a macro to fix that.
  40.  */
  41. #include <mem.h>
  42. #define bzero(b, length) memset(b, 0, length)
  43. #define bcopy(src, dest, length) memmove(dest, src, length)
  44.  
  45. /*
  46.  * NTP protocol parameters.  See section 3.2.6 of the specification.
  47.  */
  48. #define    NTP_VERSION    2    /* current version number */
  49. #define    NTP_OLDVERSION    1    /* previous version number */
  50. #define    NTP_PORT    123    /* included for sake of non-unix machines */
  51. #define    NTP_INFIN    15    /* max stratum, infinity a la Bellman-Ford */
  52. #define    NTP_MAXAGE    86400L    /* one day in seconds */
  53. #define    NTP_MAXSKW    0x28f    /* 0.01 sec in fp format */
  54. #define    NTP_MINDIST    0x51f    /* 0.02 sec in fp format */
  55. #define    NTP_MINPOLL    6    /* actually 1<<6, or 64 sec */
  56. #define    NTP_MAXPOLL    10    /* actually 1<<10, or 1024 sec */
  57. #define    NTP_WINDOW    8    /* reachability register size */
  58. #define    NTP_MAXWGT    (8*FP_SECOND)    /* maximum select weight 8 seconds */
  59. #define    NTP_MAXLIST    5    /* maximum select list size */
  60. #define    NTP_MAXSTRA    2    /* maximum number of stata in select list */
  61. #define    NTP_SELECT    (3*(256/4))    /* select weight, see code */
  62. #define    NTP_MAXKEY    65535    /* maximum authentication key number */
  63.  
  64. #define    PEER_SHIFT    8    /* 8 suitable for crystal time base */
  65. #define    PEER_MAXDISP    (64*FP_SECOND)    /* maximum dispersion (fp 64) */
  66. #define    PEER_THRESHOLD    (FP_SECOND>>1)    /* filter threshold (0.5 seconds) */
  67. #define    PEER_FILTER    1    /* filter weight (actually 1>>1) */
  68. #define    PEER_MAXDSPDEL    (FP_SECOND>>3)    /* maximum delay disp (0.125 seconds */
  69.  
  70.  
  71. /*
  72.  * Loop filter parameters.  See section 5.1 of the specification.
  73.  *
  74.  * Note that these are appropriate for a crystal time base.  If your
  75.  * system clock is line frequency controlled you should read the
  76.  * specification for appropriate modifications.  Note that the
  77.  * loop filter code will have to change if you change CLOCK_MAX
  78.  * to be greater than or equal to 500 ms.
  79.  */
  80. #define    CLOCK_UPDATE    8    /* update interval: 64 seconds */
  81. #define    CLOCK_ADJ    2    /* adjustment interval: 4 seconds */
  82. #define    CLOCK_FREQ    10    /* frequency weight: 2**10 */
  83. #define    CLOCK_PHASE    8    /* phase weight: 2**8 */
  84. #define    CLOCK_TRACK    8    /* compliance weight: 2**8 */
  85. #define    CLOCK_COMP    4    /* compliance maximum: 2**4 */
  86. #define    CLOCK_FACTOR    18    /* compliance factor: 2**18 */
  87.  
  88. #define    CLOCK_MAX_F    0x20c49ba6L    /* 128 ms, in time stamp format */
  89. #define    CLOCK_MAX_I    0x0        /* both fractional and integral parts */
  90.  
  91. #define    CLOCK_WAYTOOBIG    1000        /* if clock 1000 sec off, forget it */
  92.  
  93. /*
  94.  * Unspecified default.  sys.precision defaults to -6 unless otherwise
  95.  * adjusted.
  96.  */
  97. #define    DEFAULT_SYS_PRECISION    (-6)
  98.  
  99.  
  100. #if 0                /* Use NOS timers instead. */
  101. /*
  102.  * Event timers are actually implemented as a sorted queue of expiry
  103.  * times.  The queue is slotted, with each slot holding timers which
  104.  * expire in a 2**(NTP_MINPOLL-1) (32) second period.  The timers in
  105.  * each slot are sorted by increasing expiry time.  The number of
  106.  * slots is 2**(NTP_MAXPOLL-(NTP_MINPOLL-1)), or 32, to cover a time
  107.  * period of 2**NTP_MAXPOLL (1024) seconds into the future before
  108.  * wrapping.
  109.  */
  110.  
  111. struct event {
  112.     struct event *next;        /* next in chain */
  113.     struct event *prev;        /* previous in chain */
  114.     struct peer *peer;        /* peer this counter belongs to */
  115.     void (*event_handler)();    /* routine to call to handle event */
  116.     u_long event_time;        /* expiry time of counter */
  117. };
  118.  
  119. #define    TIMER_SLOTTIME    (1<<(NTP_MINPOLL-1))
  120. #define    TIMER_NSLOTS    (1<<(NTP_MAXPOLL-(NTP_MINPOLL-1)))
  121. #define    TIMER_SLOT(t)    (((t) >> (NTP_MINPOLL-1)) & (TIMER_NSLOTS-1))
  122.  
  123. /*
  124.  * TIMER_ENQUEUE() puts stuff on the timer queue.  It takes as
  125.  * arguments (ea), an array of event slots, and (iev), the event
  126.  * to be inserted.  This one searches the hash bucket from the
  127.  * end, and is about optimum for the timing requirements of
  128.  * NTP peers.
  129.  */
  130. #define    TIMER_ENQUEUE(ea, iev) \
  131.     do { \
  132.         register struct event *ev; \
  133.         \
  134.         ev = (ea)[TIMER_SLOT((iev)->event_time)].prev; \
  135.         while (ev->event_time > (iev)->event_time) \
  136.             ev = ev->prev; \
  137.         (iev)->prev = ev; \
  138.         (iev)->next = ev->next; \
  139.         (ev)->next->prev = (iev); \
  140.         (ev)->next = (iev); \
  141.     } while(0)
  142.  
  143. /*
  144.  * TIMER_INSERT() also puts stuff on the timer queue, but searches the
  145.  * bucket from the top.  This is better for things that do very short
  146.  * time outs, like clock support.
  147.  */
  148. #define    TIMER_INSERT(ea, iev) \
  149.     do { \
  150.         register struct event *ev; \
  151.         \
  152.         ev = (ea)[TIMER_SLOT((iev)->event_time)].next; \
  153.         while (ev->event_time != 0 && \
  154.             ev->event_time < (iev)->event_time) \
  155.             ev = ev->next; \
  156.         (iev)->next = ev; \
  157.         (iev)->prev = ev->prev; \
  158.         (ev)->prev->next = (iev); \
  159.         (ev)->prev = (iev); \
  160.     } while(0)
  161.  
  162. /*
  163.  * Remove an event from the queue.
  164.  */
  165. #define    TIMER_DEQUEUE(ev) \
  166.     do { \
  167.         if ((ev)->next != 0) { \
  168.             (ev)->next->prev = (ev)->prev; \
  169.             (ev)->prev->next = (ev)->next; \
  170.             (ev)->next = (ev)->prev = 0; \
  171.         } \
  172.     } while (0)
  173.  
  174. #endif  /* 0 */
  175.  
  176. #define    EVENT_TIMEOUT    CLOCK_ADJ
  177. /* Convert from seconds to NOS ticks.  Assumes that MSPTICK << 1000. */
  178. #define SEC2TICKS(sec)  ( ((int32)(sec)) * (1000L / MSPTICK) )
  179.  
  180. /*
  181.  * XXX
  182.  * xntpd's current_time variable replaced by calls to NOS' secclock().
  183.  */
  184. #define current_time (secclock())
  185.  
  186. /*
  187.  * The interface structure is used to hold the addresses and socket
  188.  * numbers of each of the interfaces we are using.
  189.  */
  190. struct interface {
  191.     struct sockaddr_in sin;    /* sock address in network byte order */
  192.     struct socket sock;    /* socket this is opened on */
  193.     struct udp_cb *cb;    /* UDP control block */
  194.     struct sockaddr_in bcast; /* bcast socket in network byte order */
  195.     struct socket bsock;    /* broadcast socket. */
  196.     struct sockaddr_in mask; /* interface mask */
  197.     char name[8];        /* name of interface */
  198.     int flags;        /* interface flags */
  199.     long received;        /* number of incoming packets */
  200.     long sent;        /* number of outgoing packets */
  201.     long notsent;        /* number of send failures */
  202. };
  203.  
  204. /*
  205.  * Flags for interfaces
  206.  */
  207. #define    INT_BROADCAST    1    /* can broadcast out this interface */
  208. #define    INT_BCASTOPEN    2    /* broadcast socket is open */
  209. #define    INT_LOOPBACK    4    /* the loopback interface */
  210.  
  211.  
  212. /*
  213.  * The peer structure.  Holds state information relating to the guys
  214.  * we are peering with.  Most of this stuff is from section 3.2 of the
  215.  * spec.
  216.  */
  217. struct peer {
  218.     struct peer *next;
  219.     struct peer *ass_next;        /* link pointer in associd hash */
  220.     struct sockaddr_in srcadr;    /* address of remote host */
  221.     struct interface *dstadr;    /* pointer to address on local host */
  222.     u_char leap;            /* leap indicator */
  223.     u_char hmode;            /* association mode with this peer */
  224.     u_char pmode;            /* peer's association mode */
  225.     u_char stratum;            /* stratum of remote peer */
  226.     u_char ppoll;            /* peer polling interval */
  227.     s_char precision;        /* peer's clock precision */
  228.     u_char hpoll;            /* local host's poll interval */
  229.     u_char version;            /* version indicator (XXX delete) */
  230.     u_char reach;            /* reachability, NTP_WINDOW bits */
  231.     u_char flags;            /* peer flags */
  232.     u_char refclktype;        /* reference clock type */
  233.     u_char refclkunit;        /* reference clock unit number */
  234.     u_fp distance;            /* distance from primary clock */
  235.     u_fp dispersion;        /* peer clock dispersion */
  236.     u_long refid;            /* peer reference ID */
  237.     l_fp reftime;            /* time of peer's last update */
  238.     struct timer event_timer;    /* NOS timer structure. */
  239.     u_long keyid;            /* encription key ID */
  240.     u_long pkeyid;            /* keyid used to encrypt last message */
  241.     u_short associd;        /* association ID, a unique integer */
  242.     u_char unused;
  243. /* **Start of clear-to-zero area.*** */
  244. /* Everything that is cleared to zero goes below here */
  245.     u_char valid;            /* valid counter */
  246. #define    clear_to_zero    valid
  247.     u_char trust;            /* trust, PEER_SHIFT bits */
  248.     u_char unreach;            /* unreachable count */
  249.     u_short filter_nextpt;        /* index into filter shift register */
  250.     u_fp filter_delay[PEER_SHIFT];    /* delay part of shift register */
  251.     l_fp filter_offset[PEER_SHIFT];    /* offset part of shift register */
  252.     s_fp filter_soffset[PEER_SHIFT]; /* offset in s_fp format, for disp */
  253.     l_fp org;            /* originate time stamp */
  254.     l_fp rec;            /* receive time stamp */
  255.     l_fp xmt;            /* transmit time stamp */
  256. /* ***End of clear-to-zero area.*** */
  257. /* Everything that is cleared to zero goes above here */
  258.     u_char filter_order[PEER_SHIFT]; /* we keep the filter sorted here */
  259. #define    end_clear_to_zero    filter_order[0]
  260.     u_fp estdelay;            /* filter estimated delay */
  261.     u_fp estdisp;            /* filter estimated dispersion */
  262.     l_fp estoffset;            /* filter estimated clock offset */
  263.     s_fp estsoffset;        /* fp version of above */
  264.  
  265.     /*
  266.      * Stuff related to the experimental broadcast delay
  267.      * determination code.  The registers will probably go away
  268.      * later.
  269.      */
  270.     u_long estbdelay;        /* broadcast delay, as a ts fraction */
  271.     u_long filter_bdelay[PEER_SHIFT];    /* broadcast delay registers */
  272.     u_short bdel_next;        /* where the next sample goes */
  273.     u_short bdel_ticks;        /* countdown to next transmission */
  274.     u_long bdel_time;        /* time to start next poll */
  275.  
  276.     /*
  277.      * statistic counters
  278.      */
  279.     u_long timereset;        /* time stat counters were reset */
  280.     u_long sent;            /* number of updates sent */
  281.     u_long received;        /* number of frames received */
  282.     u_long timereceived;        /* last time a frame received */
  283.     u_long timereachable;        /* last reachable/unreachable event */
  284.     u_long badlength;        /* number of funny length packets */
  285.     u_long processed;        /* processed by the protocol */
  286.     u_long badauth;            /* bad credentials detected */
  287.     u_long bogusorg;        /* rejected due to bogus origin */
  288.     u_long oldpkt;            /* rejected as duplicate packet */
  289.     u_long baddelay;        /* reject due to bad delay */
  290.     u_long seldelaytoolarge;    /* too long a delay for selection */
  291.     u_long seldisptoolarge;        /* too much dispersion for selection */
  292.     u_long selbroken;        /* broken NTP detected in selection */
  293.     u_long seltooold;        /* too long since sync in selection */
  294.     u_long untrustable;        /* not selected because not trustable */
  295.     u_char candidate;        /* position after candidate selection */
  296.     u_char falseticker;        /* position before falseticker sel */
  297.     u_char select;            /* position at end of falseticker sel */
  298.     u_char select_total;        /* number of peers in selection */
  299.     u_char was_sane;        /* set to 1 if it passed sanity check */
  300.     u_char last_event;        /* set to code for last peer error */
  301.     u_char num_events;        /* num. of events which have occurred */
  302. };
  303.  
  304. /*
  305.  * Values for peer.leap, sys_leap
  306.  */
  307. #define    LEAP_NOWARNING    0x0    /* normal, no leap second warning */
  308. #define    LEAP_ADDSECOND    0x1    /* last minute of day has 61 seconds */
  309. #define    LEAP_DELSECOND    0x2    /* last minute of day has 59 seconds */
  310. #define    LEAP_NOTINSYNC    0x3    /* overload, clock is free running */
  311.  
  312. /*
  313.  * Values for peer.mode
  314.  */
  315. #define    MODE_UNSPEC    0    /* unspecified (probably old NTP version) */
  316. #define    MODE_ACTIVE    1    /* symmetric active */
  317. #define    MODE_PASSIVE    2    /* symmetric passive */
  318. #define    MODE_CLIENT    3    /* client mode */
  319. #define    MODE_SERVER    4    /* server mode */
  320. #define    MODE_BROADCAST    5    /* broadcast mode */
  321. #define    MODE_CONTROL    6    /* control mode packet */
  322. #define    MODE_PRIVATE    7    /* implementation defined function */
  323.  
  324. #define    MODE_BCLIENT    8    /* a pseudo mode, used internally */
  325.  
  326.  
  327. /*
  328.  * Values for peer.stratum, sys_stratum
  329.  */
  330. #define    STRATUM_REFCLOCK    0    /* stratum claimed by primary clock */
  331. #define    STRATUM_PRIMARY    1        /* host has a primary clock */
  332. #define    STRATUM_INFIN    NTP_INFIN    /* infinity a la Bellman-Ford */
  333. /* A stratum of 0 in the packet is mapped to 16 internally */
  334. #define    STRATUM_PKT_UNSPEC    0    /* unspecified in packet */
  335. #define    STRATUM_UNSPEC    (NTP_INFIN+1)    /* unspecified */
  336.  
  337. /*
  338.  * Values for peer.flags
  339.  */
  340. #define    FLAG_CONFIG        0x1    /* association was configured */
  341. #define    FLAG_AUTHENABLE        0x2    /* this guy needs authentication */
  342. #define    FLAG_MINPOLL        0x4    /* keep polling interval minimized */
  343. #define    FLAG_DEFBDELAY        0x8    /* using default bdelay */
  344. #define    FLAG_AUTHENTIC        0x10    /* last message was authentic */
  345. #define    FLAG_REFCLOCK        0x20    /* this is actually a reference clock */
  346.  
  347. /*
  348.  * Definitions for the clear() routine.  We use bzero() to clear
  349.  * the parts of the peer structure which go to zero.  These are
  350.  * used to calculate the start address and length of the area.
  351.  */
  352. #define    CLEAR_TO_ZERO(p)    ((char *)&((p)->clear_to_zero))
  353. #define    END_CLEAR_TO_ZERO(p)    ((char *)&((p)->end_clear_to_zero))
  354. #define    LEN_CLEAR_TO_ZERO    (END_CLEAR_TO_ZERO((struct peer *)0) \
  355.                     - CLEAR_TO_ZERO((struct peer *)0))
  356.  
  357. /*
  358.  * Reference clock types.  Added as necessary.
  359.  */
  360. #define    REFCLK_NONE        0
  361. #define    REFCLK_LOCALCLOCK    1
  362. #define    REFCLK_WWV_HEATH    2
  363. #define    REFCLK_WWV_PST        3
  364. #define    REFCLK_WWVB_SPECTRACOM    4
  365. #define    REFCLK_GOES_TRUETIME    5
  366. #define    REFCLK_GOES_TRAK    6
  367. #define    REFCLK_CHU        7
  368.  
  369. /*
  370.  * We tell reference clocks from real peers by giving the reference
  371.  * clocks an address of the form 127.127.t.u, where t is the type and
  372.  * u is the unit number.  We define some of this here since we will need
  373.  * some sanity checks to make sure this address isn't interpretted as
  374.  * that of a normal peer.
  375.  */
  376. #define    REFCLOCK_ADDR    0x7f7f0000L    /* 127.127.0.0 */
  377. #define    REFCLOCK_MASK    0xffff0000L    /* 255.255.0.0 */
  378.  
  379. #define    ISREFCLOCKADR(srcadr)    ((SRCADR(srcadr) & REFCLOCK_MASK) \
  380.                     == REFCLOCK_ADDR)
  381.  
  382. /*
  383.  * Macro for checking for invalid addresses.  This is really, really
  384.  * gross, but is needed so no one configures a host on net 127 now that
  385.  * we're encouraging it the the configuration file.
  386.  */
  387. #define    LOOPBACKADR    ((u_long)0x7f000001)
  388. #define    LOOPNETMASK    ((u_long)0xff000000)
  389.  
  390. #define    ISBADADR(srcadr)    (((SRCADR(srcadr) & LOOPNETMASK) \
  391.                     == (LOOPBACKADR & LOOPNETMASK)) \
  392.                     && (SRCADR(srcadr) != LOOPBACKADR))
  393.  
  394. /*
  395.  * Utilities for manipulating addresses and port numbers
  396.  */
  397. #define    NSRCADR(src)    ((src)->sin_addr.s_addr) /* address in net byte order */
  398. #define    NSRCPORT(src)    ((src)->sin_port)    /* port in net byte order */
  399. #define    SRCADR(src)    (ntohl(NSRCADR((src))))    /* address in host byte order */
  400. #define    SRCPORT(src)    (ntohs(NSRCPORT((src))))    /* host port */
  401.  
  402. /*
  403.  * NTP packet format.  The mac field is optional.  It isn't really
  404.  * an l_fp either, but for now declaring it that way is convenient.
  405.  * See Appendix A in the specification.
  406.  *
  407.  * Note that all u_fp and l_fp values arrive in network byte order
  408.  * and must be converted (except the mac, which isn't, really).
  409.  */
  410. struct pkt {
  411.     u_char li_vn_mode;    /* contains leap indicator, version and mode */
  412.     u_char stratum;        /* peer's stratum */
  413.     u_char ppoll;        /* the peer polling interval */
  414.     s_char precision;    /* peer clock precision */
  415.     u_fp distance;        /* distance to primary clock */
  416.     u_fp dispersion;    /* clock dispersion */
  417.     u_long refid;        /* reference clock ID */
  418.     l_fp reftime;        /* time peer clock was last updated */
  419.     l_fp org;        /* originate time stamp */
  420.     l_fp rec;        /* receive time stamp */
  421.     l_fp xmt;        /* transmit time stamp */
  422.     u_long keyid;        /* key identification */
  423.     l_fp mac;        /* message-authentication code */
  424. };
  425.  
  426. /*
  427.  * Packets can come in two flavours, one with a mac and one without.
  428.  * These are their lengths.
  429.  */
  430. #define    MAC_LEN        (sizeof(l_fp) + sizeof(u_long))
  431. #define    LEN_PKT_MAC    sizeof(struct pkt)
  432. #define    LEN_PKT_NOMAC    (sizeof(struct pkt) - MAC_LEN)
  433.  
  434. /*
  435.  * Stuff for extracting things from li_vn_mode
  436.  */
  437. #define    PKT_MODE(li_vn_mode)    ((li_vn_mode) & 0x7)
  438. #define    PKT_VERSION(li_vn_mode)    (((li_vn_mode) >> 3) & 0x7)
  439. #define    PKT_LEAP(li_vn_mode)    (((li_vn_mode) >> 6) & 0x3)
  440.  
  441. /*
  442.  * Stuff for putting things back into li_vn_mode
  443.  */
  444. #define    PKT_LI_VN_MODE(li, vn, md) \
  445.     ((u_char)((((li) << 6) & 0xc0) | (((vn) << 3) & 0x38) | ((md) & 0x7)))
  446.  
  447.  
  448. /*
  449.  * Dealing with stratum.  0 gets mapped to 16 incoming, and back to 0
  450.  * on output.
  451.  */
  452. #define    PKT_TO_STRATUM(s)    (((s) == STRATUM_PKT_UNSPEC) ?\
  453.                  (STRATUM_UNSPEC) : (s))
  454.  
  455. #define    STRATUM_TO_PKT(s)    (((s) == (STRATUM_UNSPEC)) ?\
  456.                 (STRATUM_PKT_UNSPEC) : (s))
  457.  
  458. /*
  459.  * Format of a recvbuf.  These are used by the asynchronous receive
  460.  * routine to store incoming packets and related information.
  461.  */
  462. #define    RX_BUFF_SIZE    (120)
  463. struct recvbuf {
  464.     struct recvbuf *next;        /* next buffer in chain */
  465.     union {
  466.         struct sockaddr_in X_recv_srcadr;
  467.         caddr_t X_recv_srcclock;
  468.     } X_from_where;
  469. #define recv_srcadr    X_from_where.X_recv_srcadr
  470. #define    recv_srcclock    X_from_where.X_recv_srcclock
  471. /*    struct sockaddr_in srcadr;*/    /* where packet came from */
  472.     struct interface *dstadr;    /* interface datagram arrived thru */
  473.     l_fp recv_time;            /* time of arrival */
  474.     void (*receiver)();        /* routine to receive buffer */
  475.     int recv_length;        /* number of octets received */
  476.     union {
  477.         struct pkt X_recv_pkt;
  478.         char X_recv_buffer[RX_BUFF_SIZE];
  479.     } recv_space;
  480. #define    recv_pkt    recv_space.X_recv_pkt
  481. #define    recv_buffer    recv_space.X_recv_buffer
  482. };
  483.  
  484.  
  485. /*
  486.  * Event codes.  Used for reporting errors/events to the control module
  487.  */
  488. #define    PEER_EVENT    0x80        /* this is a peer event */
  489.  
  490. #define    EVNT_UNSPEC    0
  491. #define    EVNT_SYSRESTART    1
  492. #define    EVNT_SYSFAULT    2
  493. #define    EVNT_SYNCCHG    3
  494. #define    EVNT_PEERSTCHG    4
  495. #define    EVNT_CLOCKRESET    5
  496. #define    EVNT_BADDATETIM    6
  497. #define    EVNT_CLOCKEXCPT    7
  498.  
  499. #define    EVNT_PEERIPERR    (1|PEER_EVENT)
  500. #define    EVNT_PEERAUTH    (2|PEER_EVENT)
  501. #define    EVNT_UNREACH    (3|PEER_EVENT)
  502. #define    EVNT_REACH    (4|PEER_EVENT)
  503. #define    EVNT_PEERCLOCK    (5|PEER_EVENT)
  504. #define    EVNT_PEERSTRAT    (6|PEER_EVENT)
  505.  
  506. /*
  507.  * Clock event codes
  508.  */
  509. #define    CEVNT_NOMINAL    0
  510. #define    CEVNT_TIMEOUT    1
  511. #define    CEVNT_BADREPLY    2
  512. #define    CEVNT_FAULT    3
  513. #define    CEVNT_PROP    4
  514. #define    CEVNT_BADDATE    5
  515. #define    CEVNT_BADTIME    6
  516.  
  517. /*
  518.  * Very misplaced value.  Default port through which we send traps.
  519.  */
  520. #define    TRAPPORT    18447
  521.  
  522.  
  523. /*
  524.  * To speed lookups, peers are hashed by the low order bits of the remote
  525.  * IP address.  These definitions relate to that.
  526.  */
  527. #define    HASH_SIZE    32
  528. #define    HASH_MASK    (HASH_SIZE-1)
  529. #define    HASH_ADDR(src)    ((SRCADR((src))^(SRCADR((src))>>8)) & HASH_MASK)
  530.  
  531.  
  532. /*
  533.  * The poll update procedure takes an extra argument which controls
  534.  * how a random perturbation is applied to peer.timer.  The choice is
  535.  * to not randomize at all, to randomize only if we're going to update
  536.  * peer.timer, and to randomize no matter what (almost, the algorithm
  537.  * is that we apply the random value if it is less than the current
  538.  * timer count).
  539.  */
  540. #define    POLL_NOTRANDOM        0    /* don't randomize */
  541. #define    POLL_RANDOMCHANGE    1    /* if you change, change randomly */
  542. #define    POLL_MAKERANDOM        2    /* randomize next interval */
  543.  
  544.  
  545. /*
  546.  * How we randomize polls.  The poll interval is a power of two.
  547.  * We chose a random value which is between 1/4 and 3/4 of the
  548.  * poll interval we would normally use and which is an even multiple
  549.  * of the EVENT_TIMEOUT.  The random number routine, given an argument
  550.  * spread value of n, returns an integer between 0 and (1<<n)-1.  This
  551.  * is shifted by EVENT_TIMEOUT and added to the base value.
  552.  */
  553. #define    RANDOM_SPREAD(poll)    ((poll) - (EVENT_TIMEOUT+1))
  554. #define    RANDOM_POLL(poll, rval)    ((((rval)+1)<<EVENT_TIMEOUT) + (1<<((poll)-2)))
  555.  
  556. /*
  557.  * min, min3 and max.  Makes it easier to transliterate the spec without
  558.  * thinking about it.
  559.  */
  560. #define    min(a,b)    (((a) < (b)) ? (a) : (b))
  561. #define    max(a,b)    (((a) > (b)) ? (a) : (b))
  562. #define    min3(a,b,c)    min(min((a),(b)), (c))
  563.  
  564.  
  565. /*
  566.  * Configuration items.  These are for the protocol module (proto_config())
  567.  */
  568. #define    PROTO_BROADCLIENT    1
  569. #define    PROTO_PRECISION        2
  570. #define    PROTO_AUTHENTICATE    3
  571. #define    PROTO_BROADDELAY    4
  572. #define    PROTO_AUTHDELAY        5
  573. #define    PROTO_MAXSKEW        6
  574. #define    PROTO_SELECT        7
  575.  
  576. /*
  577.  * Configuration items for the loop filter
  578.  */
  579. #define    LOOP_DRIFTCOMP        1
  580.  
  581. /*
  582.  * Configuration items for the stats printer
  583.  */
  584. #define    STATS_FREQ_FILE        1
  585. #define STATS_FREQ        2
  586.  
  587. /*
  588.  * Default parameters.  We use these in the absense of something better.
  589.  */
  590. #define    DEFPRECISION    (-5)        /* conservatively low */
  591. #define    DEFBROADDELAY    (0x020c49baL)    /* 8 ms.  This is round trip delay */
  592.  
  593.  
  594. #ifdef XNTP_MONITOR
  595. /*
  596.  * Structure used optionally for monitoring when this is turned on.
  597.  */
  598. struct mon_data {
  599.     struct mon_data *hash_next;    /* next structure in hash list */
  600.     struct mon_data *hash_prev;    /* previous structure in hash list */
  601.     struct mon_data *mru_next;    /* next structure in MRU list */
  602.     struct mon_data *mru_prev;    /* previous structure in MRU list */
  603.     u_long lasttime;        /* last time data updated */
  604.     u_long firsttime;        /* time structure initialized */
  605.     u_long count;            /* count we have seen */
  606.     u_long rmtadr;            /* address of remote host */
  607.     u_short rmtport;        /* remote port last came from */
  608.     u_char mode;            /* mode of incoming packet */
  609.     u_char version;            /* version of incoming packet */
  610. };
  611. #endif  /* XNTP_MONITOR */
  612.  
  613. /*
  614.  * Structure used for restrictlist entries
  615.  */
  616. struct restrictlist {
  617.     struct restrictlist *next;    /* link to next entry */
  618.     u_long addr;            /* host address (host byte order) */
  619.     u_long mask;            /* mask for address (host byte order) */
  620.     u_long count;            /* number of packets matched */
  621.     u_short flags;            /* accesslist flags */
  622.     u_short mflags;            /* match flags */
  623. };
  624.  
  625. /*
  626.  * Access flags
  627.  */
  628. #define    RES_IGNORE        0x1    /* ignore if matched */
  629. #define    RES_DONTSERVE        0x2    /* don't give him any time */
  630. #define    RES_DONTTRUST        0x4    /* don't trust if matched */
  631. #define    RES_NOQUERY        0x8    /* don't allow queries if matched */
  632. #define    RES_NOMODIFY        0x10    /* don't allow him to modify server */
  633. #define    RES_NOPEER        0x20    /* don't allocate memory resources */
  634. #define    RES_NOTRAP        0x40    /* don't allow him to set traps */
  635. #define    RES_LPTRAP        0x80    /* traps set by him are low priority */
  636.  
  637. #define    RES_ALLFLAGS \
  638.     (RES_IGNORE|RES_DONTSERVE|RES_DONTTRUST|RES_NOQUERY\
  639.     |RES_NOMODIFY|RES_NOPEER|RES_NOTRAP|RES_LPTRAP)
  640.  
  641. /*
  642.  * Match flags
  643.  */
  644. #define    RESM_INTERFACE        0x1    /* this is an interface */
  645. #define    RESM_NTPONLY        0x2    /* match ntp port only */
  646.  
  647. /*
  648.  * Restriction configuration ops
  649.  */
  650. #define    RESTRICT_FLAGS        1    /* add flags to restrict entry */
  651. #define    RESTRICT_UNFLAG        2    /* remove flags from restrict entry */
  652. #define    RESTRICT_REMOVE        3    /* remove a restrict entry */
  653.  
  654.  
  655. /*
  656.  * Experimental alternate selection algorithm identifiers
  657.  */
  658. #define    SELECT_1    1
  659. #define    SELECT_2    2
  660. #define    SELECT_3    3
  661. #define    SELECT_4    4
  662. #define    SELECT_5    5
  663.